﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Db4objects.Db4o;
using Db4objects.Db4o.Config;
using Db4objects.Db4o.Query;
using Db4objects.Db4o.Events;

using System.Linq;
using Db4objects.Db4o.Linq;

using NLog;

using System.Text;
using System.IO;



namespace Layers.Tests
{
    using MbUnit.Framework;

    [TestFixture]
    public class TestBase
    {
        public IObjectContainer DB;

        [NonSerialized]
        public NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

        public TestBase()
        {
        }

        public virtual string DBFileName
        {
            get { return this.GetType().Name+DateTime.Now.ToShortDateString()+".yap"; }
        }

        public virtual IConfiguration DatabaseConfiguration()
        {
            IConfiguration cfg = Db4oFactory.NewConfiguration();
            //cfg.CallConstructors(true);
            //cfg.TestConstructors(false);
            cfg.Add(new Db4objects.Db4o.TA.TransparentPersistenceSupport());
            
            return cfg;
        }

        public virtual IObjectContainer OpenDatabase(bool delete)
        {
            if(delete)
                File.Delete(DBFileName);
            IObjectContainer db = Db4oFactory.OpenFile(DatabaseConfiguration(), DBFileName);
            EventRegistryFactory.ForObjectContainer(db).Activated+=new ObjectEventHandler(DB_Activated);
            return db;
        }

        protected virtual void  DB_Activated(object sender, ObjectEventArgs args)
        {
            WriteLine("DB_Activated {0}: {1}",args.Object.GetType().Name,args.Object.ToString()); 	        
        }

        public void WriteLine(string msg, params object[] args)
        {
            Log.Info(msg, args);
            Console.WriteLine(msg, args);
        }
        
        public void Write(string msg, params object[] args)
        {
            Log.Info(msg, args);
            Console.Write(msg, args);
        }


        [Test]
        public void RunAll()
        {
            Log.Info("\n\nTest {0}\n\n", this.GetType().Name);
            Console.WriteLine("\n\nTest {0}\n\n", this.GetType().Name);

            All();
            
            Console.WriteLine("\nDone....\n");
            Console.ReadKey();
        }

        public virtual void All()
        {
        }
    }
}